package com.pig4cloud.mall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.pig4cloud.mall.product.dto.CategoryTree;
import com.pig4cloud.mall.product.dto.CidTree;
import com.pig4cloud.mall.product.entity.Category;
import com.pig4cloud.mall.product.enums.Level;
import com.pig4cloud.mall.product.mapper.CategoryMapper;
import com.pig4cloud.mall.product.service.CategoryService;
import com.pig4cloud.mall.product.util.TreeUtil;
import com.pig4cloud.pig.common.core.constant.CommonConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 商品三级分类
 *
 * @author fangyu
 * @date 2020-11-25 15:05:09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

	/**
	 * 构建树查询 1. 不是懒加载情况，查询全部 2. 是懒加载，根据parentId 查询 2.1 父节点为空，则查询ID -1
	 * @param lazy 是否是懒加载
	 * @param parentId 父节点ID
	 * @return
	 */
	@Override
	public List<CategoryTree> treeCategory(boolean lazy, Integer parentId) {
		if (!lazy) {
			return TreeUtil.buildTree(
					baseMapper.selectList(Wrappers.<Category>lambdaQuery().orderByAsc(Category::getSort)),
					CommonConstants.CATEGORY_TREE_ROOT_ID);
		}

		// 懒加载
		Long parent = parentId == null ? CommonConstants.CATEGORY_TREE_ROOT_ID : parentId;
		return lazyTreeCategory(parent);
	}

	@Override
	public List<CidTree> treeCategoryCid(Integer depth) {
		if (depth > Level.THREE.getLevel()) {
			log.error("超过树最大深度");
			return Collections.emptyList();
		}
		return TreeUtil.buildTreeCid(
				baseMapper.selectList(Wrappers.<Category>lambdaQuery().orderByAsc(Category::getSort)),
				CommonConstants.CATEGORY_TREE_ROOT_ID, depth);
	}

	@Override
	public Boolean addCategory(Category category) {
		if (category.getParentCid() == null) {
			category.setParentCid(CommonConstants.CATEGORY_TREE_ROOT_ID);
			category.setCatLevel(Level.ONE.getLevel());
		} else {
			Category result = baseMapper.selectById(category.getParentCid());
			category.setCatLevel(result.getCatLevel()+1);
		}
		if (category.getSort() == null) {
			category.setSort(0);
		}
		// TODO 不能保存4级
		if (category.getCatLevel() > Level.THREE.getLevel()) {
			return false;
		}
		return SqlHelper.retBool(baseMapper.insert(category));
	}

	/**
	 * 查询当前父节点的子数据
	 *
	 * @param parentId 父节点id
	 * @return
	 */
	public List<CategoryTree> lazyTreeCategory(Long parentId) {
		List<Category> categories = baseMapper.selectList(Wrappers.<Category>lambdaQuery().eq(Category::getParentCid, parentId).orderByAsc(Category::getSort));
		List<CategoryTree> trees = new ArrayList<>();
		CategoryTree categoryTree;
		for (Category category: categories) {
			categoryTree = new CategoryTree(category);
			categoryTree.setHasChildren(queryHasChild(category.getCatId()));
			trees.add(categoryTree);
		}
		return trees;
	}

	/**
	 * 查询是否包含子节点
	 * @param catId 节点id
	 * @return
	 */
	public boolean queryHasChild(Long catId) {
		return baseMapper.selectList(Wrappers.<Category>lambdaQuery().eq(Category::getParentCid, catId)).size() != 0;
	}
}
